#include <iostream>
#include <algorithm>

using namespace std;

typedef struct  {
	int time;
	int value;
} YAO;

int cache[100][1000];

int dp(YAO y[], int size, int t, int from)
{
	if (t<=0)
	{
		return 0;
	}
	
	if (from == size)
	{
		return 0;
	}
	
	if (t < y[from].time)
	{
		cache[from][t] = dp(y, size, t, from+1);
		
//		printf("t=%d, from=%d, sum=%d\r\n", t, from, cache[from][t]);
		
		return cache[from][t];
	}
	
	// 采
	int yes = y[from].value + dp(y, size, t-y[from].time, from+1);
	
	// 不采
	int no = dp(y, size, t, from+1);
	
	cache[from][t] = max(yes, no);
	
//	printf("t=%d, from=%d, sum=%d\r\n", t, from, cache[from][t]);
	
	return cache[from][t];
}

int main(int argc, char *argv[]) {
	int t, m, sum;
	cin >> t >> m;
	
	YAO y[m];
	
	memset(cache, 0, sizeof(int)*1000*100);
	
	for (int i=0;i<m;++i) {
		cin >> y[i].time >> y[i].value;
	}
		
	cout << dp(y, m, t, 0) << endl;
	
	return 0;
}